from Netease_cloud_lyric_download import *
import re

time_tag = []
lyric_dict = {}
lines = []
def lrc_to_dict():
    global lines
    global time_tag
    global lyric_dict

    with open('1.lrc', 'r', encoding='utf8') as f:
        lines1 = f.readlines()


        # 去空行
        for kong in lines1:
            if kong == '\n':
                lines1.remove(kong)

        print(lines1)

        key_word = ['.by:.+\\n']
        for a in key_word:
            for i in lines1:
                rs = re.findall(a, i)
                if rs != []:
                    print(rs)
                    lines1.remove(rs[0])

        print(lines1)

        for i in lines1:
            i = i[0:6]+ ',' +i[7:]
            lines.append(i)

        #del lines[0]

        #生成字典
        if lines[0][9] == ']':
            index = 9
        else:
            index = 10
        lyric_dict = {lines[i][1:index]:lines[i][index+1:] for i in range(len(lines))}

        #get_time_tag():
        for i in lines:
            time_tag.append(i[1:index])



def get_srt(time_tag, end_time):
    with open('3.srt', 'w', encoding='utf8') as f:

        for num in range(len(time_tag)):
            if num == len(time_tag)-1:
                f.write(f'{num + 1}\n')
                f.write(f'{time_tag[num]} --> {end_time}\n')
                f.write(f'{lyric_dict[time_tag[num]]}\n')
                break
            f.write(f'{num+1}\n')
            f.write(f'{time_tag[num]} --> {time_tag[num+1]}\n')
            f.write(f'{lyric_dict[time_tag[num]]}\n')


if __name__ == '__main__':
    flag = int(input('是否要下载歌词(1为下载)'))
    if flag == 1:
        song_id = input('网易云歌曲id：')  # 1398628590
        lyric = lyric_downloader(song_id)
        save_lyric(lyric)
    lrc_to_dict()
    print(time_tag)
    print(lyric_dict)
    end_time = input('结束时间：格式示范（03:40,398）') #02:40,373
    get_srt(time_tag, end_time)

